home *** CD-ROM | disk | FTP | other *** search
- /*
- File: GradientImport.c
-
- Copyright (c) 1990, Thomas Knoll.
- Copyright (c) 1993-6, Adobe Systems Incorporated.
- All rights reserved.
-
- C source file for import example.
- */
-
- #if __MWERKS__
- #include <SetupA4.h> // A4-globals
- #include <A4Stuff.h> // A4-globals
- #endif
-
- #if defined(THINK_C) || defined(__MWERKS__)
- #define ENTRYPOINT main
- #endif
-
- #include "GradientImport.h"
-
- Handle hDllInstance = NULL;
-
- /*****************************************************************************/
- void InitGlobals (GPtr globals);
- void ValidateParameters (GPtr globals);
- void CreateDescriptor (GPtr globals);
- void SwitchScriptInfo (GPtr globals, PIReadDescriptor readToken);
- void DoPrepare (GPtr globals);
- void DoStart (GPtr globals);
- void DoContinue (GPtr globals);
- void DoFinish (GPtr globals);
- void DoFinalize (GPtr globals);
- /*****************************************************************************/
-
- /* All calls to the plug-in module come through this routine. It must be
- placed first in the resource. To achieve this, most development systems
- require that this be the first routine in the source. */
-
- #if MSWindows
- void ENTRYPOINT (short selector,
- AcquireRecord *acquireParamBlock,
- long *data,
- short *result)
- #else
- pascal void ENTRYPOINT (short selector,
- AcquireRecord *acquireParamBlock,
- long *data,
- short *result)
- #endif
- {
-
- /* We copy the globals out of the handle into a stack allocated area
- so that we don't have to worry about the handle moving. */
-
- GPtr globals;
- Globals globalsValue;
-
- #if __MWERKS__
- EnterCodeResource(); // A4-globals
- #endif
-
- if (!*data)
- {
-
- InitGlobals (&globalsValue);
-
- *data = (long) NewHandle (sizeof (Globals));
-
- if (!*data)
- {
- *result = memFullErr;
- return;
- }
-
- ** (GHdl) *data = globalsValue;
-
- }
-
- globalsValue = ** (GHdl) *data;
-
- globals = &globalsValue;
-
- gStuff = acquireParamBlock;
- gResult = noErr;
-
- switch (selector)
- {
-
- case acquireSelectorAbout:
- DoAbout (globals);
- break;
-
- case acquireSelectorPrepare:
- DoPrepare (globals);
- break;
-
- case acquireSelectorStart:
- DoStart (globals);
- break;
-
- case acquireSelectorContinue:
- DoContinue (globals);
- break;
-
- case acquireSelectorFinish:
- DoFinish (globals);
- break;
-
- case acquireSelectorFinalize:
- DoFinalize (globals);
- break;
-
- default:
- gResult = acquireBadParameters;
-
- }
-
- *result = gResult;
- ** (GHdl) *data = globalsValue;
-
- #if __MWERKS__
- ExitCodeResource(); // A4-globals
- #endif
-
- }
-
- /*****************************************************************************/
-
- /* We can initialize our globals here, but since we don't know if we'll always
- get this call, it's better to flag them and check that they're valid before
- we first use them. */
-
- void InitGlobals (GPtr globals)
- {
-
- gInitParameters = true;
- gQueryForParameters = true;
-
- }
-
- /*****************************************************************************/
-
- /* Check parameters and make sure their valid, or initialize them. */
-
- void ValidateParameters (GPtr globals)
- {
- short loop;
-
- if (gInitParameters)
- {
- gDialog = NULL;
-
- #if Macintosh
- gDialogHdl = NULL;
- #endif
-
- gLastImages = 0;
- gLastRows = 256;
- gLastCols = 256;
- gLastMode = plugInModeRGBColor;
- gLastInvert = false;
- gContinueImport = false;
-
- gInitParameters = false;
-
- gCount = 0;
- gToken = NULL;
-
- for (loop = 0; loop < kMaxDescriptors; loop++)
- gArray [loop] = NULL;
- }
- }
-
- /*****************************************************************************/
-
- /* Prepare to acquire an image. If the plug-in module needs a large amount
- of buffer memory, this routine should set the maxData field to the
- number of bytes required. Since we are going to use the bufferProcs,
- we simply set maxData to zero. */
-
- void DoPrepare (GPtr globals)
- {
-
- gStuff->maxData = 0;
-
- if (!WarnBufferProcsAvailable ())
- gResult = userCanceledErr; // exit gracefully
-
- /* If finalization is available, we will want it. */
-
- gStuff->wantFinalize = true;
-
- ValidateParameters (globals);
- /* if stuff hasn't been initialized that we need, do it,
- then go check if we've got scripting commands to
- override our settings */
- OpenScriptParams (globals); // update our parameters with the scripting parameters, if available
-
- }
-
-
- /*****************************************************************************/
-
- void DoStart (GPtr globals)
- {
-
- int16 j;
-
- /* Insist on having the buffer procs. */
-
- if (!WarnBufferProcsAvailable ())
- {
- gResult = userCanceledErr; // exit gracefully
- return;
- }
-
- /* Assume we won't be coming back around for another pass unless explicitly so. */
-
- gStuff->acquireAgain = gContinueImport = false;
-
- /* If we need to use our dialog, or it's already up, do it. */
-
- ValidateParameters (globals);
- ReadScriptParams (globals);
-
- if (gQueryForParameters)
- {
- /* Open the dialog. If it is already up, this does nothing. */
-
- if (!OpenOurDialog (globals))
- {
- gQueryForParameters = false;
- CloseScriptParams(globals);
- gResult = memFullErr;
- return;
- }
-
- /* Run the parameters dialog. */
-
- if (!RunOurDialog (globals))
- {
-
- gQueryForParameters = false;
-
- CloseOurDialog (globals);
- CloseScriptParams(globals);
-
- gResult = userCanceledErr;
- //gStuff->wantFinalize = true;
- return;
-
- }
- else
- {
-
- /* Still going! */
-
- gContinueImport = true;
-
- }
-
- /* If we cannot finalize, then we want to close the dialog. */
-
- if (!gStuff->canFinalize)
- {
-
- gQueryForParameters = false;
- CloseOurDialog (globals);
- CloseScriptParams(globals);
-
- }
-
-
- }
-
- /* Fill in the parameters for the document. */
-
- gStuff->imageSize.v = gLastRows;
- gStuff->imageSize.h = gLastCols;
- gStuff->imageMode = gLastMode;
-
- if (gStuff->imageMode == plugInModeBitmap)
- gStuff->depth = 1;
- else
- gStuff->depth = 8;
-
- if (gStuff->imageMode == plugInModeRGBColor)
- gStuff->planes = 3;
- else
- gStuff->planes = 1;
-
- if (gStuff->depth == 1)
- {
- gStuff->imageHRes = FixRatio (300, 1);
- gStuff->imageVRes = FixRatio (300, 1);
- }
- else
- {
- gStuff->imageHRes = FixRatio (72, 1);
- gStuff->imageVRes = FixRatio (72, 1);
- }
-
- if (gStuff->imageMode == plugInModeIndexedColor)
- for (j = 0; j < 256; j++)
- {
- gStuff->redLUT [j] = (unsigned8) j;
- gStuff->greenLUT [j] = (unsigned8) (255 - j);
- gStuff->blueLUT [j] = (unsigned8) j;
- }
-
- gStuff->data = nil;
- }
-
- /*****************************************************************************/
-
- #define RANGE_ITER(lower,upper,first,last,step) \
- for (lower = (first); \
- (upper = (((lower) + (step) < (last)) ? (lower) + (step) : (last))), \
- lower < (last); \
- lower = upper)
-
- /*****************************************************************************/
-
- void DoContinue (GPtr globals)
- {
-
- AcquireRegion region;
- AcquireDataLayout layout;
- Ptr data;
- int32 chunkRows;
- BufferID buffer = 0;
- int16 maxHeight;
- int16 actualHeight;
-
- long done = 0;
- long total = (long) gStuff->imageSize.v * (long) gStuff->planes;
-
- /* Scale total for post-processing. */
-
- if (gLastInvert && gStuff->canReadBack)
- total *= 3;
-
- /* Set up the layout. */
-
- if (gStuff->depth == 8)
- layout.rowBytes = gStuff->imageSize.h;
- else
- layout.rowBytes = (gStuff->imageSize.h + 7) >> 3;
- layout.colBytes = 1;
- layout.planeBytes = 0; /* irrelevant -- we work one plane at a time */
-
- /* Allocate the buffer and start acquisition. We limit the buffer
- height to keep the space consumed by this plug-in down. We base
- this estimate on reserving enough space for one of row of tiles
- with an assumed height of 300 pixels. */
-
- maxHeight = (int16) ((BufferSpace () / layout.rowBytes) - 300);
-
- if (maxHeight > gStuff->imageSize.v)
- maxHeight = gStuff->imageSize.v;
- else if (maxHeight < 1)
- maxHeight = 1;
-
- gResult = AllocateStripBuffer (layout.rowBytes,
- 1,
- maxHeight,
- 1,
- &actualHeight,
- &buffer);
-
- chunkRows = actualHeight;
-
- if (gResult != noErr)
- goto exit;
-
- data = LockBuffer (buffer, false);
-
- region.rect.left = 0;
- region.rect.right = gStuff->imageSize.h;
-
- for (region.loPlane = 0; region.loPlane < gStuff->planes; ++region.loPlane)
- {
-
- region.hiPlane = region.loPlane;
-
- RANGE_ITER (region.rect.top, region.rect.bottom,
- 0, gStuff->imageSize.v, chunkRows)
- {
-
- Ptr p;
- int16 subRow;
-
- for (subRow = region.rect.top, p = data;
- subRow < region.rect.bottom;
- ++subRow, p += layout.rowBytes)
- {
-
- int16 col;
- Ptr q;
-
- if (TestAbort ())
- {
- gResult = userCanceledErr;
- goto exit;
- }
-
- UpdateProgress (done++, total);
-
- for (col = 0, q = p; col < layout.rowBytes; ++col, ++q)
- {
-
- if (gStuff->depth == 1)
- *q = 0xFF << (subRow & 7);
- else if (gStuff->planes == 1)
- *q = subRow + col;
- else if (region.loPlane == 0)
- *q = (char) col;
- else if (region.loPlane == 1)
- *q = (char ) subRow;
- else
- *q = subRow + col;
-
- }
-
- }
-
- gResult = StoreData (gStuff, data, ®ion, &layout);
-
- if (gResult != noErr)
- goto exit;
-
- }
-
- }
-
- if (gLastInvert && gStuff->canReadBack)
- {
-
- for (region.loPlane = 0; region.loPlane < gStuff->planes; ++region.loPlane)
- {
-
- region.hiPlane = region.loPlane;
-
- RANGE_ITER (region.rect.top, region.rect.bottom,
- 0, gStuff->imageSize.v, chunkRows)
- {
-
- Ptr p;
- int16 subRow;
-
- gResult = FetchData (gStuff, data, ®ion, &layout);
-
- if (gResult != noErr)
- goto exit;
-
- done += region.rect.bottom - region.rect.top;
-
- for (subRow = region.rect.top, p = data;
- subRow < region.rect.bottom;
- ++subRow, p += layout.rowBytes)
- {
-
- int16 col;
- Ptr q;
-
- if (TestAbort ())
- {
- gResult = userCanceledErr;
- goto exit;
- }
-
- UpdateProgress (done++, total);
-
- for (col = 0, q = p; col < layout.rowBytes; ++col, ++q)
- *q = ~*q;
-
- }
-
- gResult = StoreData (gStuff, data, ®ion, &layout);
-
- if (gResult != noErr)
- goto exit;
-
- }
-
- }
-
- }
-
- gStuff->data = nil;
- PISetRect (&gStuff->theRect, 0, 0, 0, 0);
-
- exit:
-
- if (buffer != 0)
- FreeBuffer (buffer);
-
- }
-
- /*****************************************************************************/
-
- /* This routine will always be called if DoStart does not return an error
- (even if DoContinue returns an error or the user aborts the operation).
- This allows the module to perform any needed cleanup. */
-
- void DoFinish (GPtr globals)
- {
-
- gStuff->acquireAgain = gContinueImport;
-
- /* Store scripting parameters in object descriptor */
- CreateDescriptor(globals);
-
- /* Write our parameters if we can't finalize */
-
- if (!gStuff->canFinalize)
- CheckAndWriteScriptParams(globals); // writes script params
-
- }
-
- /*****************************************************************************/
-
- void DoFinalize (GPtr globals)
- {
-
- gQueryForParameters = false;
- CloseOurDialog (globals);
-
- /* Write our parameters */
-
- CheckAndWriteScriptParams(globals); // writes script params
-
- }
-
- /*****************************************************************************/
-
- OSErr StoreData (AcquireRecord *stuff,
- void *data,
- AcquireRegion *region,
- AcquireDataLayout *layout)
- {
-
- OSErr result = noErr;
-
- if (!WarnHostAdvanceStateAvailable (stuff->advanceState, hDllInstance))
- return userCanceledErr; // exit gracefully
-
- stuff->data = data;
- stuff->theRect = region->rect;
- stuff->loPlane = region->loPlane;
- stuff->hiPlane = region->hiPlane;
- stuff->rowBytes = layout->rowBytes;
- stuff->colBytes = (short)(layout->colBytes);
- stuff->planeBytes = layout->planeBytes;
-
- stuff->wantReadBack = false;
-
- result = (*(stuff->advanceState)) ();
-
- stuff->data = NULL;
-
- return result;
-
- }
-
- /*****************************************************************************/
-
- OSErr FetchData (AcquireRecord *stuff,
- void *data,
- AcquireRegion *region,
- AcquireDataLayout *layout)
- {
-
- OSErr result = noErr;
-
- if (!WarnHostAdvanceStateAvailable (stuff->advanceState, hDllInstance))
- return userCanceledErr; // exit gracefully
-
- if (!stuff->canReadBack)
- return errPlugInHostInsufficient;
-
- stuff->data = data;
- stuff->theRect = region->rect;
- stuff->loPlane = region->loPlane;
- stuff->hiPlane = region->hiPlane;
- stuff->rowBytes = layout->rowBytes;
- stuff->colBytes = (short)(layout->colBytes);
- stuff->planeBytes = layout->planeBytes;
-
- stuff->wantReadBack = true;
-
- result = (*(stuff->advanceState)) ();
-
- stuff->data = NULL;
- stuff->wantReadBack = false;
-
- return result;
-
- }
-
- /*****************************************************************************/
-